<?php
namespace app\mainadmin\model;
/**
 * @Author: Mr.Mao
 * @Date:   2021-05-31 10:29:52
 * @Last Modified by:   Mr.Mao
 * @Last Modified time: 2021-06-02 16:32:15
 */
use app\BaseModel;
use think\facade\Cache;
use think\facade\Env;
//*------------------------------------------------------ */
//-- 存储配置
/*------------------------------------------------------ */
class StorageModel extends BaseModel
{
	protected $table = 'main_storage';
	public  $pk = 'id';
	protected static $mkey = 'main_storage_';
	/**
	 * [getRows 获取列表]
	 */
	public function getRows()
	{
		$data = [];
		$res = $this->where('is_use',1)->select()->toArray();
		foreach ($res as $key => $value) {
			$value['def_config'] = [];
			if (empty($value['type']) == false) {
				$file = Env::get('extend_path')."/storage/".$value['type']."/config.php";
	            $def_config = include $file;
				$value['def_config'] = $def_config['config'];
				if (empty($value['config']) == false) {
					$value['config'] = json_decode($value['config'],true);
					$value['is_config'] = 1;
				}else{
					$value['is_config'] = 0;
				}
				
			}
			$data[$value['id']] = $value;
		}
		return $data;
		
	}

	/**
	 * [getRow 获取详情]
	 */
	public function getRow($id)
	{
		$where[] = ['is_use','=',1];
		$where[] = ['id','=',$id];
		$res = $this->where($where)->find();

		if(empty($res) == false){
			$data = $res->toArray();
			$data['def_config'] = [];
			if (empty($data['type']) == false) {
				$file = Env::get('extend_path')."/storage/".$data['type']."/config.php";
		        $def_config = include $file;
				$data['def_config'] = $def_config['config'];
				$data['config'] = json_decode($data['config'],true);
			}
			
		}
		return $data;
		
	}

	/**
	 * [getInfo 获取详情]
	 * @param  array  $where [条件]
	 */
	public function getInfo($where=[])
	{
		$where[] = ['is_use','=',1];
		$res = $this->where($where)->find();

		if(empty($res) == false){
			$data = $res->toArray();
			$data['def_config'] = [];
			if (empty($data['type']) == false) {
				$file = Env::get('extend_path')."/storage/".$data['type']."/config.php";
		        $def_config = include $file;
				$data['def_config'] = $def_config['config'];
				$data['config'] = json_decode($data['config'],true);
			}
			
		}
		return $data;
	}
}
